home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / andere sprachen / gamesmaster / demosrc / c / random / random.c < prev    next >
C/C++ Source or Header  |  1996-07-16  |  3KB  |  109 lines

  1. /*
  2. ** Random Demo
  3. ** -----------
  4. ** Demonstrates the randomness of functions FastRandom() and SlowRandom().
  5. ** Given the speed of FastRandom I think you will be quite impressed with
  6. ** it's results.  It gets all the numbers and gives a very even balance.
  7. ** The sequence of numbers is however not the best, so only use it when
  8. ** speed is critical.
  9. **
  10. ** In case you are wondering the FastRandom() routine is just 10 assembler
  11. ** instructions including the re-seed and range div...  Wow!
  12. **
  13. ** Compiles under SAS/C.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <proto/games.h>
  19. #include <proto/exec.h>
  20.  
  21. struct GMSBase *GMSBase;
  22. int YesNo;
  23. UWORD Number;
  24. ULONG loop, Iterations, UserRange, *NumArray;
  25.  
  26. /*=========================================================================*/
  27. /*                               MAIN CODE                                 */
  28. /*=========================================================================*/
  29.  
  30. void main(void)
  31. {
  32.    struct GamesLibrary *GMSBase = (struct GamesLibrary *)
  33.        OpenLibrary("games.library", 0);
  34.    if (GMSBase == NULL) exit(FALSE);
  35.  
  36.    printf(" Random Number Demo");
  37.    printf("\n------------------");
  38.    printf("\nThis program demonstrates the randomness of the games.library functions");
  39.    printf("\nFastRandom() and SlowRandom().  The FastRandom() is just 10 assembler");
  40.    printf("\ninstructions and SlowRandom() is about twice that.  Both give very good");
  41.    printf("\nresults, as you will see...\n\n");
  42.  
  43.    do
  44.     {
  45.       printf("Please enter a range between 1 and 500 : ");
  46.       fflush(stdin);
  47.       scanf("%d", &UserRange);
  48.     }
  49.    while (UserRange > 500 || UserRange < 1);
  50.  
  51.    printf("Enter the amount of iterations (Try 500000) : ");
  52.    fflush(stdin);
  53.    scanf("%d", &Iterations);
  54.  
  55.    printf("Do you want to view all the generated numbers (Y/n)? ");
  56.    fflush(stdin);
  57.    if (getchar() == 'n') YesNo = 1;
  58.  
  59.    SetUserPri();
  60.  
  61. /*=========================================================================*/
  62. /*                     GENERATE FAST RANDOM NUMBERS                        */
  63. /*=========================================================================*/
  64.  
  65.    NumArray = malloc(sizeof(UWORD));
  66.    for (loop=0; loop<UserRange; loop++)
  67.       NumArray[loop] = 0;
  68.  
  69.    printf("\nPlease wait while generating %d FastRandom() numbers...\n",Iterations);
  70.    do
  71.     {
  72.       Number = FastRandom(UserRange);
  73.       if (YesNo == 0) printf("%d ", Number);
  74.       NumArray[Number]++;
  75.     }
  76.    while (loop++ < Iterations);
  77.  
  78.    printf("\n\nResults are in...\n");
  79.    for (loop=0; loop<UserRange; loop++)
  80.     {
  81.       printf("%4d: %d\n", loop, NumArray[loop]);
  82.       NumArray[loop] = 0;
  83.     };
  84.  
  85. /*=========================================================================*/
  86. /*                     GENERATE SLOW RANDOM NUMBERS                        */
  87. /*=========================================================================*/
  88.  
  89.    printf("\nPlease wait while generating %d SlowRandom() numbers...\n",Iterations);
  90.    do
  91.     {
  92.       Number = SlowRandom(UserRange);
  93.       if (YesNo == 0) printf("%d ",Number);
  94.       NumArray[Number]++;
  95.     }
  96.    while (loop++ < Iterations);
  97.  
  98.    printf("\n\nResults are in...\n");
  99.    for (loop=0; loop<UserRange; loop++)
  100.     {
  101.       printf("%4d: %d\n", loop, NumArray[loop]);
  102.     };
  103.  
  104.    CloseLibrary((struct Library *)GMSBase);
  105. }
  106.  
  107. /*=========================================================================*/
  108.  
  109.